package Swing;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIcon_Demo extends JFrame {
    public ImageIcon_Demo() {
        //获取图片地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIcon_Demo.class.getResource("F:\\untitled\\src\\1.PNG");//获得同级目录下的文件
        ImageIcon imageIcon = new ImageIcon(url);//命名对象
        label.setIcon(imageIcon);//设置图片对象
        label.setHorizontalAlignment(SwingConstants.CENTER);//居中显示

        Container contentPane = getContentPane();
        contentPane.add(label);
        setBounds(200,200,400,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new ImageIcon_Demo();
    }
}
